package model

import (
	"gorm.io/gorm"
)

// Patient 患者模型
type Patient struct {
	gorm.Model
	Name           string `gorm:"type:varchar(50);not null;comment:姓名"`
	Phone          string `gorm:"type:varchar(11);not null;unique;comment:手机号"`
	Password       string `gorm:"type:char(64);not null;comment:密码"`
	Salt           string `gorm:"type:char(32);not null;comment:盐"`
	AvatarUrl      string `gorm:"type:varchar(255);comment:头像URL"`
	IdCard         string `gorm:"type:varchar(18);comment:身份证号"`
	RealName       string `gorm:"type:varchar(50);comment:真实姓名"`
	IsVerified     bool   `gorm:"type:tinyint(1);not null;default:0;comment:是否实名认证"`
	HealthRecord   string `gorm:"type:text;comment:健康档案"`
	AllergyHistory string `gorm:"type:text;comment:过敏史"`
	MedicalHistory string `gorm:"type:text;comment:病史"`
	Status         int    `gorm:"type:tinyint(1);not null;default:1;comment:状态:1-正常,2-禁用"`
}

// RegisterPatient 注册患者
func (p *Patient) RegisterPatient(db *gorm.DB) error {
	return db.Create(&p).Error
}

// GetPatientByPhone 根据手机号查询患者
func (p *Patient) GetPatientByPhone(db *gorm.DB, phone string) error {
	return db.Where("phone = ?", phone).First(&p).Error
}

// GetPatientByID 根据ID查询患者
func (p *Patient) GetPatientByID(db *gorm.DB, id int64) error {
	return db.Where("id = ?", id).First(&p).Error
}

// UpdatePatient 更新患者信息
func (p *Patient) UpdatePatient(db *gorm.DB) error {
	return db.Save(&p).Error
}

